package com.ihg.hiex.admin.service.session.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.ihg.hiex.admin.dao.session.DelegateCoachingConversationDao;
import com.ihg.hiex.admin.service.DataServiceImpl;
import com.ihg.hiex.admin.service.session.DelegateCoachingConversationService;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.user.Delegate;
import com.ihg.hiex.entity.user.DelegateCoachingConversation;

/**
 * 
 * @author dan
 *
 */
@Service("delegateCoachingConversationService")
public class DelegateCoachingConversationServiceImpl extends DataServiceImpl<DelegateCoachingConversation> implements DelegateCoachingConversationService{
    
    @Dao
    private DelegateCoachingConversationDao delegateCoachingConversationDao;

    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.DelegateCoachingConversationService#listConversation(Session)
     */
    public Map<String, List<DelegateCoachingConversation>> listConversation(Session session) {
        List<DelegateCoachingConversation> conversations = delegateCoachingConversationDao.list(session);
        return CollectionUtils.constructMapValues(conversations, "delegate.id");
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.DelegateCoachingConversationService#listConversation(java.util.List)
     */
    public Map<String, List<DelegateCoachingConversation>> listConversation(List<Delegate> delegates) {
        if(!delegates.isEmpty()) {
            List<DelegateCoachingConversation> conversations = delegateCoachingConversationDao.list(delegates);
            return CollectionUtils.constructMapValues(conversations, "delegate.id");
        }
        return null;
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.DelegateCoachingConversationService#list(com.ihg.hiex.entity.user.Delegate)
     */
    public List<DelegateCoachingConversation> list(Delegate delegate) {
        return delegateCoachingConversationDao.list(delegate);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.DelegateCoachingConversationService#generateConversationAmount(java.util.Map)
     */
    public Map<String, Map<String, Integer>> generateConversationAmount(
            Map<String, List<DelegateCoachingConversation>> conversations) {
        
        if(conversations != null && !conversations.isEmpty()) {
            Map<String, Map<String, Integer>> result = new HashMap<String, Map<String, Integer>>();
            for(Entry<String, List<DelegateCoachingConversation>> conversation : conversations.entrySet()) {
                Map<String, Integer> resultItem = new HashMap<String, Integer>();
                resultItem.put("total", conversation.getValue().size());
                
                int finish = 0;
                for(DelegateCoachingConversation delegateCoachingConversation : conversation.getValue()) {
                    if(delegateCoachingConversation.getStatus().isConfirmed()) {
                        finish = finish + 1;
                    }
                }
                resultItem.put("finish", finish);
                result.put(conversation.getKey(), resultItem);
            }
            return result;
        }
        return null;
    }

}
